package com.weiyin.examination.course;

import android.app.Activity;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.weiyin.examination.R;
import com.weiyin.examination.adapter.BottomAdapter;
import com.weiyin.examination.application.Constant;
import com.weiyin.examination.application.ResId;
import com.weiyin.examination.dialog.CourseA1Dialog;
import com.weiyin.examination.dialog.FlatSharpDialog;
import com.weiyin.examination.http.RetrofitClient;
import com.weiyin.examination.listener.CourseInterface;
import com.weiyin.examination.listener.DialogResultInterface;
import com.weiyin.examination.listener.DialogShowListener;
import com.weiyin.examination.module.AnswerModule;
import com.weiyin.examination.module.AnswerXml;
import com.weiyin.examination.module.BitMapModule;
import com.weiyin.examination.module.BottomModule;
import com.weiyin.examination.module.Note;
import com.weiyin.examination.module.SelectionModule;
import com.weiyin.examination.module.TestModule;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.LogUtil;
import com.weiyin.examination.util.SPUtil;
import com.weiyin.examination.view.CourseView;
import com.weiyin.examination.view.GridLayoutManager;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author njf
 *         Created by lenovo on 2017/9/18.
 */

public class CourseA5 extends Course implements BottomAdapter.OnItemClickInterface, CourseView.CourseViewListener {
    private View rootView;
    private Activity activity;
    private ClickAble clickAble;

    private RecyclerView bottomRecyclerView;
    private Map<String, String> map, leftMap;

    private TextView titleLayoutNextTextView, titleLayoutLastTextView, titleLayoutSubjectContentTextView,
            titleLayoutSubjectScoreTextView, titleLayoutNumberTextView, titleTicketNumber,
            courseA5RlBigSolveRlTextView1, courseA5RlBigSolveRlTextView2,
            courseA5RlBig1SolveRlTextView1, courseA5RlBig1SolveRlTextView2,
            courseA5RlBig2SolveRlTextView1, courseA5RlBig2SolveRlTextView2,
            courseA5RlBig3SolveRlTextView1, courseA5RlBig3SolveRlTextView2,
            courseA5RlBig4SolveRlTextView1, courseA5RlBig4SolveRlTextView2;
    private RelativeLayout titleLayoutNumberRl, courseA5RlBig, courseA5RlBig1, courseA5RlBig2, courseA5RlBig3, courseA5RlBig4,
            courseA5RlBigSolveRl, courseA5RlBig1SolveRl, courseA5RlBig2SolveRl, courseA5RlBig3SolveRl, courseA5RlBig4SolveRl;
    private ImageView courseA5RlBigFlatSharp, courseA5RlBig1FlatSharp, courseA5RlBig2FlatSharp,
            courseA5RlBig3FlatSharp, courseA5RlBig4FlatSharp, courseA5BigTextView, courseA5Big1TextView, courseA5Big2TextView, courseA5Big3TextView, courseA5Big4TextView;
    private CourseView courseA5RlBigCourseView, courseA5RlBig1CourseView, courseA5RlBig2CourseView, courseA5RlBig3CourseView, courseA5RlBig4CourseView;
    private TestModule testModule;
    private List<TestModule> testModuleList;
    private List<AnswerModule> answerModuleList;
    private int courseIndex;
    private int position = 0;
    private List<ImageView> imageViews;

    private Handler mHandler = new Handler();
    private boolean click = false;
    private BottomAdapter bottomAdapter;
    private boolean clef = true;

    private int[] timeImageId = {R.drawable.course_time_signature24, R.drawable.course_time_signature34,
            R.drawable.course_time_signature38, R.drawable.course_time_signature44,
            R.drawable.course_time_signature68};
    private List<BottomModule> integerList;
    private boolean spot = false, homoPhonicConnection = false, isRepose = false, threeNote = false;
    private int mark = 0;
    private View view, view1;
    private LayoutHelper layoutHelper;
    private ResId resId;
    private int riseDropIndex = -1;
    private List<BitMapModule> bitMapModuleList, bitMapModuleList1, bitMapModuleList2, bitMapModuleList3, bitMapModuleList4;
    private List<List<BitMapModule>> bitMapModuleLists;
    private List<CourseView> courseViewList;
    private int bitmapModuleIndex = 0;

    private double score;

    @Override
    public void onCreate(Activity activity, ViewFlipper contain, List<TestModule> courseEntities, int index) {
        if (rootView == null) {
            rootView = LayoutInflater.from(activity).inflate(R.layout.coursea5_layout, null);
            layoutHelper = new LayoutHelper(activity);
            layoutHelper.scaleView(rootView);

            contain.addView(rootView);
            this.testModuleList = courseEntities;
            this.activity = activity;
            this.courseIndex = index;
            this.testModule = testModuleList.get(index);
            clickAble = new ClickAble();
            resId = new ResId();
            imageViews = new ArrayList<>();
            map = new HashMap<>();
            leftMap = new HashMap<>();
            getImage();
            if (testModule != null) {
                if (testModule.getRightAnswerList() != null) {
                    this.answerModuleList = testModule.getRightAnswerList();
                }
            }
            initView();
        }
    }

    private void getImage() {
        if (integerList == null) {
            integerList = new ArrayList<>();
        }
        for (int i = 0; i < resId.getImageId().length; i++) {
            BottomModule bottomModule = new BottomModule();
            bottomModule.setBitmapId(resId.getImageId()[i]);
            bottomModule.setGreyBitmapId(resId.getGreyImageId()[i]);
            if (i == 0 || i == 2 || i == 3 || i == 4 || i == 5 || i == 11 || i == 12 || i == 13 || i == 14 || i == 15) {
                bottomModule.setClick(false);
            } else {
                bottomModule.setClick(true);
            }
            integerList.add(bottomModule);
        }
    }

    @Override
    public void initView() {
        courseA5RlBigFlatSharp = (ImageView) rootView.findViewById(R.id.courseA5RlBigFlatSharp);
        courseA5RlBig1FlatSharp = (ImageView) rootView.findViewById(R.id.courseA5RlBig1FlatSharp);
        courseA5RlBig2FlatSharp = (ImageView) rootView.findViewById(R.id.courseA5RlBig2FlatSharp);
        courseA5RlBig3FlatSharp = (ImageView) rootView.findViewById(R.id.courseA5RlBig3FlatSharp);
        courseA5RlBig4FlatSharp = (ImageView) rootView.findViewById(R.id.courseA5RlBig4FlatSharp);

        courseA5BigTextView = (ImageView) rootView.findViewById(R.id.courseA5RlBigTextView);
        courseA5Big1TextView = (ImageView) rootView.findViewById(R.id.courseA5RlBig1TextView);
        courseA5Big2TextView = (ImageView) rootView.findViewById(R.id.courseA5RlBig2TextView);
        courseA5Big3TextView = (ImageView) rootView.findViewById(R.id.courseA5RlBig3TextView);
        courseA5Big4TextView = (ImageView) rootView.findViewById(R.id.courseA5RlBig4TextView);

        imageViews.add(courseA5BigTextView);
        imageViews.add(courseA5Big1TextView);
        imageViews.add(courseA5Big2TextView);
        imageViews.add(courseA5Big3TextView);
        imageViews.add(courseA5Big4TextView);

        titleLayoutNextTextView = (TextView) rootView.findViewById(R.id.titleLayoutNextTextView);
        titleLayoutLastTextView = (TextView) rootView.findViewById(R.id.titleLayoutLastTextView);
        titleLayoutSubjectContentTextView = (TextView) rootView.findViewById(R.id.titleLayoutSubjectContentTextView);
        titleLayoutSubjectScoreTextView = (TextView) rootView.findViewById(R.id.titleLayoutSubjectScoreTextView);
        titleLayoutNumberTextView = (TextView) rootView.findViewById(R.id.titleLayoutNumberTextView);
        courseA5RlBigSolveRlTextView1 = (TextView) rootView.findViewById(R.id.courseA5RlBigSolveRlTextView1);
        courseA5RlBigSolveRlTextView2 = (TextView) rootView.findViewById(R.id.courseA5RlBigSolveRlTextView2);
        courseA5RlBig1SolveRlTextView1 = (TextView) rootView.findViewById(R.id.courseA5RlBig1SolveRlTextView1);
        courseA5RlBig1SolveRlTextView2 = (TextView) rootView.findViewById(R.id.courseA5RlBig1SolveRlTextView2);
        courseA5RlBig2SolveRlTextView1 = (TextView) rootView.findViewById(R.id.courseA5RlBig2SolveRlTextView1);
        courseA5RlBig2SolveRlTextView2 = (TextView) rootView.findViewById(R.id.courseA5RlBig2SolveRlTextView2);
        courseA5RlBig3SolveRlTextView1 = (TextView) rootView.findViewById(R.id.courseA5RlBig3SolveRlTextView1);
        courseA5RlBig3SolveRlTextView2 = (TextView) rootView.findViewById(R.id.courseA5RlBig3SolveRlTextView2);
        courseA5RlBig4SolveRlTextView1 = (TextView) rootView.findViewById(R.id.courseA5RlBig4SolveRlTextView1);
        courseA5RlBig4SolveRlTextView2 = (TextView) rootView.findViewById(R.id.courseA5RlBig4SolveRlTextView2);

        courseA5RlBigCourseView = (CourseView) rootView.findViewById(R.id.courseA5RlBigCourseView);
        courseA5RlBig1CourseView = (CourseView) rootView.findViewById(R.id.courseA5RlBig1CourseView);
        courseA5RlBig2CourseView = (CourseView) rootView.findViewById(R.id.courseA5RlBig2CourseView);
        courseA5RlBig3CourseView = (CourseView) rootView.findViewById(R.id.courseA5RlBig3CourseView);
        courseA5RlBig4CourseView = (CourseView) rootView.findViewById(R.id.courseA5RlBig4CourseView);

        titleTicketNumber = (TextView) rootView.findViewById(R.id.titleTicketNumber);
        courseA5RlBig = (RelativeLayout) rootView.findViewById(R.id.courseA5RlBig);
        courseA5RlBig1 = (RelativeLayout) rootView.findViewById(R.id.courseA5RlBig1);
        courseA5RlBig2 = (RelativeLayout) rootView.findViewById(R.id.courseA5RlBig2);
        courseA5RlBig3 = (RelativeLayout) rootView.findViewById(R.id.courseA5RlBig3);
        courseA5RlBig4 = (RelativeLayout) rootView.findViewById(R.id.courseA5RlBig4);

        titleLayoutNumberRl = (RelativeLayout) rootView.findViewById(R.id.titleLayoutNumberRl);

        courseA5RlBigSolveRl = (RelativeLayout) rootView.findViewById(R.id.courseA5RlBigSolveRl);
        courseA5RlBig1SolveRl = (RelativeLayout) rootView.findViewById(R.id.courseA5RlBig1SolveRl);
        courseA5RlBig2SolveRl = (RelativeLayout) rootView.findViewById(R.id.courseA5RlBig2SolveRl);
        courseA5RlBig3SolveRl = (RelativeLayout) rootView.findViewById(R.id.courseA5RlBig3SolveRl);
        courseA5RlBig4SolveRl = (RelativeLayout) rootView.findViewById(R.id.courseA5RlBig4SolveRl);


        bottomRecyclerView = (RecyclerView) rootView.findViewById(R.id.bottomRecyclerView);
        bottomAdapter = new BottomAdapter(activity, R.layout.coursea5_item_layout, integerList);
        bottomRecyclerView.setLayoutManager(new GridLayoutManager(activity, integerList.size(), LinearLayoutManager.VERTICAL, false));
        bottomRecyclerView.setAdapter(bottomAdapter);
        bottomAdapter.setOnItemClickInterface(this);

        courseViewList = new ArrayList<>();
        bitMapModuleLists = new ArrayList<>();
        bitMapModuleList = new ArrayList<>();
        bitMapModuleList1 = new ArrayList<>();
        bitMapModuleList2 = new ArrayList<>();
        bitMapModuleList3 = new ArrayList<>();
        bitMapModuleList4 = new ArrayList<>();
        bitMapModuleLists.add(bitMapModuleList);
        bitMapModuleLists.add(bitMapModuleList1);
        bitMapModuleLists.add(bitMapModuleList2);
        bitMapModuleLists.add(bitMapModuleList3);
        bitMapModuleLists.add(bitMapModuleList4);

        courseA5RlBigCourseView.setBitMapModuleList(bitMapModuleList);
        courseA5RlBig1CourseView.setBitMapModuleList(bitMapModuleList1);
        courseA5RlBig2CourseView.setBitMapModuleList(bitMapModuleList2);
        courseA5RlBig3CourseView.setBitMapModuleList(bitMapModuleList3);
        courseA5RlBig4CourseView.setBitMapModuleList(bitMapModuleList4);

        courseViewList.add(courseA5RlBigCourseView);
        courseViewList.add(courseA5RlBig1CourseView);
        courseViewList.add(courseA5RlBig2CourseView);
        courseViewList.add(courseA5RlBig3CourseView);
        courseViewList.add(courseA5RlBig4CourseView);
        for (int i = 0; i < courseViewList.size(); i++) {
            courseViewList.get(i).setCourseA5(true);
            courseViewList.get(i).setLoadSize(2);
            courseViewList.get(i).setCourseViewListener(this);
        }

        initListener();
        setVisible();
    }

    private void initListener() {
        courseA5RlBigSolveRl.setOnClickListener(clickAble);
        courseA5RlBig1SolveRl.setOnClickListener(clickAble);
        courseA5RlBig2SolveRl.setOnClickListener(clickAble);
        courseA5RlBig3SolveRl.setOnClickListener(clickAble);
        courseA5RlBig4SolveRl.setOnClickListener(clickAble);

        titleLayoutNextTextView.setOnClickListener(clickAble);
        titleLayoutLastTextView.setOnClickListener(clickAble);
        titleLayoutNumberRl.setOnClickListener(clickAble);
        courseA5RlBigFlatSharp.setOnClickListener(clickAble);
        courseA5RlBig1FlatSharp.setOnClickListener(clickAble);
        courseA5RlBig2FlatSharp.setOnClickListener(clickAble);
        courseA5RlBig3FlatSharp.setOnClickListener(clickAble);
        courseA5RlBig4FlatSharp.setOnClickListener(clickAble);
    }

    private void initViewData() {
        if (testModule != null) {
            titleTicketNumber.setText("准考证号：" + "" + testModule.getTickNumber());

            titleLayoutLastTextView.setVisibility(courseIndex == 0 ? View.GONE : View.VISIBLE);
            titleLayoutNextTextView.setVisibility(courseIndex == testModuleList.size() - 1 ? View.GONE : View.VISIBLE);

            titleLayoutSubjectContentTextView.setText((courseIndex + 1) + "丶" + testModule.getSubjectTitle());
            titleLayoutSubjectScoreTextView.setText("（" + testModule.getScore() + "分）");
            titleLayoutNumberTextView.setText("第" + (courseIndex + 1) + "题/第" + (testModuleList.size()) + "题");

            for (int i = 0; i < testModule.getSelectionList().size(); i++) {
                SelectionModule selectionModule = testModule.getSelectionList().get(i);
                if (i < imageViews.size()) {
                    if (selectionModule != null) {
                        Glide.with(activity).
                                load(RetrofitClient.BASE_URL + selectionModule.getImageUrl()).
                                into(imageViews.get(i));
                    }
                }
            }
        }
    }

    private void showFlatSharpDialog(final int position, final ImageView view, boolean type) {
        FlatSharpDialog flatSharpDialog = new FlatSharpDialog(activity, R.style.base_loading_dialogfragment, type);
        Window window = flatSharpDialog.getWindow();
        assert window != null;
        WindowManager.LayoutParams p = window.getAttributes();
        Display d = window.getWindowManager().getDefaultDisplay();
        window.setGravity(Gravity.LEFT | Gravity.TOP);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        // 新位置X坐标
        p.x = location[0] - view.getWidth() / 2;
        // 新位置Y坐标
        p.y = location[1];
        window.setAttributes(p);
        flatSharpDialog.show();
        flatSharpDialog.setDialogResultInterface(new DialogResultInterface() {
            @Override
            public void result(String type, String result, String answerId) {
                riseDropIndex = Integer.valueOf(result);
                if (Constant.COURSE_A9_DIALOG_RESULT1.equals(type)) {
                    view.setImageResource(resId.getFateId_2()[riseDropIndex]);
                    courseViewList.get(bitmapModuleIndex).setFlatIndex(riseDropIndex);
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    int fifths;
                    if (!clef) {
                        if (riseDropIndex < 7) {
                            fifths = -1 - riseDropIndex;
                            layoutParams.topMargin = layoutHelper.scaleDimension(130, "y");
                        } else {
                            fifths = riseDropIndex - 6;
                            layoutParams.topMargin = layoutHelper.scaleDimension(123, "y");
                        }
                    } else {
                        if (riseDropIndex < 7) {
                            fifths = -1 - riseDropIndex;
                            layoutParams.topMargin = layoutHelper.scaleDimension(130, "y");
                        } else {
                            fifths = riseDropIndex - 6;
                            layoutParams.topMargin = layoutHelper.scaleDimension(115, "y");
                        }
                    }
                    map.put(position + "", fifths + "");
                } else if (Constant.COURSE_A9_DIALOG_RESULT2.equals(type)) {
                    view.setImageResource(timeImageId[Integer.valueOf(result)]);
                    if (riseDropIndex == 0 || riseDropIndex == 1 || riseDropIndex == 3) {
                        courseViewList.get(bitmapModuleIndex).setNoteType(4);
                        courseViewList.get(bitmapModuleIndex).setLineType(true);
                    } else {
                        courseViewList.get(bitmapModuleIndex).setNoteType(3);
                        courseViewList.get(bitmapModuleIndex).setLineType(true);
                    }
                }
            }
        });
    }

    private void setClick(boolean setCurImg) {
        if (view != null) {
            if (setCurImg) {
                view.setSelected(false);
            }
            if (!view.isSelected()) {
                LogUtil.i(TAG, " click = false");
                click = false;
                if (setCurImg)
                    courseViewList.get(bitmapModuleIndex).setCurImg(17, click, isRepose, spot, mark, homoPhonicConnection, threeNote, clef);
            }
        }
    }

    @Override
    public void onItemClick(BaseViewHolder holder, int position) {
        View viewLayout = holder.getView(R.id.courseA5ItemLayout);
        switch (position) {
            case 0:
                break;
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                click = true;
                viewLayout.setSelected(!viewLayout.isSelected());
                if (view != null && view.getX() != viewLayout.getX()) {
                    view.setSelected(false);
                } else {
                    setClick(false);
                }
                view = viewLayout;
                break;
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
                if (mark != position) {
                    mark = position;
                } else {
                    mark = 0;
                }
                viewLayout.setSelected(!viewLayout.isSelected());
                if (view1 != null && view1.getX() != viewLayout.getX()) {
                    view1.setSelected(false);
                }
                view1 = viewLayout;
                break;
            case 11:
                spot = !spot;
                threeNote = false;
                homoPhonicConnection = false;
                viewLayout.setSelected(!viewLayout.isSelected());
                break;
            case 13:
                threeNote = !threeNote;
                spot = false;
                homoPhonicConnection = false;
                viewLayout.setSelected(!viewLayout.isSelected());
                break;
            case 14:
                spot = false;
                threeNote = false;
                homoPhonicConnection = false;
                viewLayout.setSelected(!viewLayout.isSelected());
                break;
            case 15:
                spot = false;
                threeNote = false;
                homoPhonicConnection = !homoPhonicConnection;
                viewLayout.setSelected(!viewLayout.isSelected());
                break;
            case 16:
                break;
            case 12:
                viewLayout.setSelected(!viewLayout.isSelected());
                if (!isRepose) {
                    isRepose = true;
                    for (int i = 0; i < resId.getReposeId().length; i++) {
                        integerList.get(i + 1).setBitmapId(resId.getReposeId()[i]);
                    }
                    bottomAdapter.notifyDataSetChanged();
                } else {
                    isRepose = false;
                    for (int i = 0; i < resId.getReposeId().length; i++) {
                        integerList.get(i + 1).setBitmapId(resId.getImageId()[i + 1]);
                    }
                    bottomAdapter.notifyDataSetChanged();
                }
                break;
            default:
                viewLayout.setSelected(!viewLayout.isSelected());
                break;
        }
        this.position = position;
        courseViewList.get(bitmapModuleIndex).setCurImg(position, click, isRepose, spot, mark, homoPhonicConnection, threeNote, clef);
    }

    private void courseA5RlBigVisible() {
        courseA5RlBig.setVisibility(View.VISIBLE);
        courseA5RlBig1.setVisibility(View.GONE);
        courseA5RlBig2.setVisibility(View.GONE);
        courseA5RlBig3.setVisibility(View.GONE);
        courseA5RlBig4.setVisibility(View.GONE);
    }

    private void courseA5RlBig1Visible() {
        courseA5RlBig.setVisibility(View.GONE);
        courseA5RlBig1.setVisibility(View.VISIBLE);
        courseA5RlBig2.setVisibility(View.GONE);
        courseA5RlBig3.setVisibility(View.GONE);
        courseA5RlBig4.setVisibility(View.GONE);
    }

    private void courseA5RlBig2Visible() {
        courseA5RlBig.setVisibility(View.GONE);
        courseA5RlBig1.setVisibility(View.GONE);
        courseA5RlBig2.setVisibility(View.VISIBLE);
        courseA5RlBig3.setVisibility(View.GONE);
        courseA5RlBig4.setVisibility(View.GONE);
    }

    private void courseA5RlBig3Visible() {
        courseA5RlBig.setVisibility(View.GONE);
        courseA5RlBig1.setVisibility(View.GONE);
        courseA5RlBig2.setVisibility(View.GONE);
        courseA5RlBig3.setVisibility(View.VISIBLE);
        courseA5RlBig4.setVisibility(View.GONE);
    }

    /**/
    private void courseA5RlBig4Visible() {
        courseA5RlBig.setVisibility(View.GONE);
        courseA5RlBig1.setVisibility(View.GONE);
        courseA5RlBig2.setVisibility(View.GONE);
        courseA5RlBig3.setVisibility(View.GONE);
        courseA5RlBig4.setVisibility(View.VISIBLE);
    }

    private void setVisible() {
        switch (bitmapModuleIndex) {
            case 0:
                courseA5RlBigVisible();
                break;
            case 1:
                courseA5RlBig1Visible();
                break;
            case 2:
                courseA5RlBig2Visible();
                break;
            case 3:
                courseA5RlBig3Visible();
                break;
            case 4:
                courseA5RlBig4Visible();
                break;
            default:
                break;
        }
    }

    @Override
    public void courseViewResult(int type) {
        LogUtil.i(TAG, "type=" + type);
        if (1 == type) {
            mark = 0;
            if (view1 != null) {
                view1.setSelected(false);
            }
            LogUtil.i(TAG, "courseViewResult");
        }
        if (type == -1) {
            bitmapModuleIndex--;
            if (bitmapModuleIndex < 0) {
                bitmapModuleIndex = 0;
            }
            setVisible();
            courseViewList.get(bitmapModuleIndex).setCurImg(position, click, isRepose, spot, mark, homoPhonicConnection, threeNote, clef);
        } else if (type == -2) {
            bitmapModuleIndex++;
            if (bitmapModuleIndex > 4) {
                bitmapModuleIndex = 4;
            }
            setVisible();
            courseViewList.get(bitmapModuleIndex).setCurImg(position, click, isRepose, spot, mark, homoPhonicConnection, threeNote, clef);
        }
        if (type == 4) {
            setClick(true);
        }
    }

    class ClickAble implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.courseA5RlBigSolveRl:
                    showCourseA1Dialog(0, courseA5RlBigSolveRlTextView1, courseA5RlBigSolveRlTextView2);
                    break;
                case R.id.courseA5RlBig1SolveRl:
                    showCourseA1Dialog(1, courseA5RlBig1SolveRlTextView1, courseA5RlBig1SolveRlTextView2);
                    break;
                case R.id.courseA5RlBig2SolveRl:
                    showCourseA1Dialog(2, courseA5RlBig2SolveRlTextView1, courseA5RlBig2SolveRlTextView2);
                    break;
                case R.id.courseA5RlBig3SolveRl:
                    showCourseA1Dialog(3, courseA5RlBig3SolveRlTextView1, courseA5RlBig3SolveRlTextView2);
                    break;
                case R.id.courseA5RlBig4SolveRl:
                    showCourseA1Dialog(4, courseA5RlBig4SolveRlTextView1, courseA5RlBig4SolveRlTextView2);
                    break;
                case R.id.courseA5RlBigFlatSharp:
                    showFlatSharpDialog(0, courseA5RlBigFlatSharp, false);
                    break;
                case R.id.courseA5RlBig1FlatSharp:
                    showFlatSharpDialog(1, courseA5RlBig1FlatSharp, false);
                    break;
                case R.id.courseA5RlBig2FlatSharp:
                    showFlatSharpDialog(2, courseA5RlBig2FlatSharp, false);
                    break;
                case R.id.courseA5RlBig3FlatSharp:
                    showFlatSharpDialog(3, courseA5RlBig3FlatSharp, false);
                    break;
                case R.id.courseA5RlBig4FlatSharp:
                    showFlatSharpDialog(4, courseA5RlBig4FlatSharp, false);
                    break;
                case R.id.titleLayoutNextTextView:
                    dialogShowListener.dialogShow(1, courseIndex, result());
                    courseInterface.courseInterfaceResult(Constant.NEXT_KNOWLEDGE, courseIndex + 1);
                    break;
                case R.id.titleLayoutLastTextView:
                    dialogShowListener.dialogShow(1, courseIndex, result());
                    courseInterface.courseInterfaceResult(Constant.LAST_KNOWLEDGE, courseIndex - 1);
                    break;
                case R.id.titleLayoutNumberRl:
                    dialogShowListener.dialogShow(1, courseIndex, result());
                    dialogShowListener.dialogShow(0, courseIndex, result());
                    break;
                default:
                    break;
            }
        }
    }

    private void showCourseA1Dialog(int pisition, final TextView textView, final TextView textView2) {
        CourseA1Dialog courseA1Dialog;
        int x = (int) courseA5RlBigSolveRl.getX() + courseA5RlBigSolveRl.getWidth() / 2;
        int y = courseA5RlBigSolveRl.getBottom() + courseA5RlBigSolveRl.getTop();
        courseA1Dialog = new CourseA1Dialog(activity, R.style.BlueToothDialogStyle, testModule);
        Window window = courseA1Dialog.getWindow();
        assert window != null;
        WindowManager.LayoutParams p = window.getAttributes();
        Display d = window.getWindowManager().getDefaultDisplay();

        p.x = x - d.getWidth() / 2 + layoutHelper.scaleDimension(0, "x");
        p.y = y - (d.getHeight() / 2 - layoutHelper.scaleDimension(230 + 100, "y") / 2);
        window.setAttributes(p);
        courseA1Dialog.setDialogResultInterface(new DialogResultInterface() {
            @Override
            public void result(String type, String result, String answerId) {
                LogUtil.i(TAG, "type=" + type + " result=" + result);
                if (Constant.CENTER.equals(type)) {
                    textView.setText(result);
                    leftMap.put(position + Constant.CENTER, answerId);
                } else if (Constant.LEFT.equals(type)) {
                    textView.setText(result);
                    leftMap.put(position + Constant.LEFT, answerId);
                } else if (Constant.RIGHT.equals(type)) {
                    if (!"空".equals(result)) {
                        textView2.setText(result);
                    } else {
                        textView2.setText("");
                    }
                    leftMap.put(position + Constant.RIGHT, answerId);
                }
            }
        });
        if (activity.hasWindowFocus()) {
            courseA1Dialog.show();
        }
    }

    private boolean result() {
        getScore();
        if (bitMapModuleList.size() > 0 || bitMapModuleList1.size() > 0 || bitMapModuleList2.size() > 0
                || bitMapModuleList3.size() > 0 || bitMapModuleList4.size() > 0 || !StringUtils.isEmpty(courseA5RlBigSolveRlTextView1.getText())
                || !StringUtils.isEmpty(courseA5RlBigSolveRlTextView2.getText()) || riseDropIndex != -1) {
            return true;
        } else {
            return false;
        }
    }

    private void getScore() {

        Constant.getScreen(activity, SPUtil.getSharedPreferencesData(activity, "number") + "_" + (courseIndex + 1),SPUtil.getSharedPreferencesData(activity, "number")+"_"+ (courseIndex + 1));
        List<AnswerXml> courseAnswer1 = courseA5RlBigCourseView.getAnswerXmlList();

        List<AnswerXml> courseAnswer2 = courseA5RlBig1CourseView.getAnswerXmlList();

        List<AnswerXml> courseAnswer3 = courseA5RlBig2CourseView.getAnswerXmlList();

        List<AnswerXml> courseAnswer4 = courseA5RlBig3CourseView.getAnswerXmlList();

        List<AnswerXml> courseAnswer5 = courseA5RlBig4CourseView.getAnswerXmlList();
        List<List<AnswerXml>> lists = new ArrayList<>();
        lists.add(courseAnswer1);
        lists.add(courseAnswer2);
        lists.add(courseAnswer3);
        lists.add(courseAnswer4);
        lists.add(courseAnswer5);
        for (int i = 0; i < answerModuleList.size(); i++) {
            List<AnswerXml> list = answerModuleList.get(i).getAnswerXmlList();
            if (i < lists.size() && i < list.size()) {
                if (list.get(0).getFifths().equals(map.get(i + ""))) {
                    score += Double.valueOf(answerModuleList.get(i).getScore()) / answerModuleList.get(0).getAnswerXmlList().size() / 4;
                }
                if (leftMap.get(i + Constant.LEFT).equals(answerModuleList.get(i).getLeftId())) {
                    score += Double.valueOf(answerModuleList.get(i).getScore()) / answerModuleList.get(0).getAnswerXmlList().size() / 4 / 2;
                }
                if (leftMap.get(i + Constant.RIGHT).equals(answerModuleList.get(i).getRightId())) {
                    score += Double.valueOf(answerModuleList.get(i).getScore()) / answerModuleList.get(0).getAnswerXmlList().size() / 4 / 2;
                }
                List<Note> noteLists = lists.get(i).get(0).getNoteList();
                List<Note> noteList = list.get(0).getNoteList();
                for (int j = 0; j < noteList.size(); j++) {
                    Note note = noteList.get(j);
                    if (note.equalsNote(noteLists.get(j), false, false)) {
                        score += Double.valueOf(answerModuleList.get(0).getScore()) / answerModuleList.get(0).getAnswerXmlList().size() / 2 / noteList.size();
                    }
                }
            }
        }
        LogUtil.i(TAG,"score="+score);
        SPUtil.setSharedPreferencesData(activity.getApplication(), CourseA5.class.getSimpleName(),score+"");
    }

    @Override
    public void setCourseInterface(CourseInterface courseInterface) {
        this.courseInterface = courseInterface;
    }

    @Override
    public void setCourseDialogListener(DialogShowListener dialogListener) {
        this.dialogShowListener = dialogListener;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    @Override
    public void show(List<TestModule> courseEntities, int index, String type) {
        initViewData();
    }

    @Override
    public void stop() {

    }

    @Override
    public void destroy() {

    }
}
